package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.api.Code;
import com.mall.common.api.Result;
import com.mall.entity.SmsCoupon;
import com.mall.service.SmsCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "SmsCouponCategoryController", description = "优惠券管理")
@RestController
@RequestMapping("/coupons")
public class SmsCouponController {

    @Resource
    private SmsCouponService smsCouponService;

    @ApiOperation(value = "新增优惠券")
    @PostMapping
    public Result save(@RequestBody SmsCoupon smsCoupon) {
        smsCoupon.setCount(smsCoupon.getPublishCount()-smsCoupon.getReceiveCount());
        boolean flag = smsCouponService.save(smsCoupon);
        return new Result(flag? Code.SAVE_OK: Code.SAVE_ERR,flag);
    }

    @ApiOperation(value = "根据id删除优惠券")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = smsCouponService.delete(id);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @ApiOperation(value = "根据id查找优惠券")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        SmsCoupon smsCoupon = new SmsCoupon();
        smsCoupon = smsCouponService.getById(id);
        Integer code = smsCoupon !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = smsCoupon !=null ? "successful":"failed,please try again";
        return new Result(smsCoupon,code,msg);
    }

    @ApiOperation(value = "查询可领取的优惠券")
    @GetMapping("/getAbleCoupon/{memberId}")
    public Result getAbleCoupon(@PathVariable Integer memberId)
    {
        List<SmsCoupon> list = smsCouponService.getAbleCoupon(memberId);
        Integer code = list !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list !=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @ApiOperation(value = "查找全部优惠券")
    @GetMapping
    public Result getAll(){
        List<SmsCoupon> list = smsCouponService.getAll();
        Integer code = list!=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean flag = smsCouponService.removeByIds(ids);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @GetMapping("/page")
    public IPage<SmsCoupon> getPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam String name) {
        IPage<SmsCoupon> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SmsCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return smsCouponService.page(page,queryWrapper);
    }
}
